<template>
  <div class="library-categories-container">
    <div class="page-header">
      <h1>图书分类</h1>
      <p>浏览不同类别的图书资源</p>
    </div>

    <div class="categories-content">
      <div class="categories-grid" v-loading="loading">
        <div 
          v-for="category in categories"
          :key="category.id"
          class="category-card"
          @click="goToCategoryBooks(category.id)"
        >
          <div class="category-icon">
            <el-icon><Folder /></el-icon>
          </div>
          <div class="category-info">
            <h3 class="category-name">{{ category.name }}</h3>
            <p class="category-description">{{ category.description }}</p>
            <div class="category-stats">
              <span class="book-count">{{ category.bookCount || 0 }} 本图书</span>
              <span class="popular-badge" v-if="category.isPopular">热门</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-if="!loading && categories.length === 0" class="empty-state">
        <el-empty description="暂无分类" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getBookCategories } from '@/api/library'

const router = useRouter()

// 响应式数据
const categories = ref([])
const loading = ref(false)

// 页面加载
onMounted(() => {
  loadCategories()
})

// 加载分类列表
const loadCategories = async () => {
  try {
    loading.value = true
    const response = await getBookCategories()
    if (response.code === 200) {
      categories.value = response.data || []
    }
  } catch (error) {
    console.error('获取分类列表失败:', error)
    ElMessage.error('获取分类列表失败')
  } finally {
    loading.value = false
  }
}

// 跳转到分类图书页面
const goToCategoryBooks = (categoryId) => {
  router.push(`/library/category/${categoryId}`)
}
</script>

<style lang="scss" scoped>
.library-categories-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20px;
}

.page-header {
  text-align: center;
  margin-bottom: 40px;
  
  h1 {
    font-size: 32px;
    font-weight: 600;
    color: #303133;
    margin: 0 0 12px 0;
  }
  
  p {
    font-size: 16px;
    color: #606266;
    margin: 0;
  }
}

.categories-content {
  max-width: 1200px;
  margin: 0 auto;
}

.categories-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
  
  .category-card {
    background: white;
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.1);
    cursor: pointer;
    transition: all 0.3s ease;
    
    &:hover {
      transform: translateY(-4px);
      box-shadow: 0 8px 24px rgba(0,0,0,0.15);
    }
    
    .category-icon {
      width: 60px;
      height: 60px;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      border-radius: 12px;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-bottom: 16px;
      
      .el-icon {
        font-size: 28px;
        color: white;
      }
    }
    
    .category-info {
      .category-name {
        font-size: 20px;
        font-weight: 600;
        color: #303133;
        margin: 0 0 8px 0;
      }
      
      .category-description {
        color: #606266;
        font-size: 14px;
        line-height: 1.5;
        margin: 0 0 16px 0;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }
      
      .category-stats {
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .book-count {
          color: #909399;
          font-size: 14px;
        }
        
        .popular-badge {
          background: #ff6b6b;
          color: white;
          padding: 2px 8px;
          border-radius: 12px;
          font-size: 12px;
        }
      }
    }
  }
}

.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 300px;
  background: white;
  border-radius: 12px;
}

@media (max-width: 768px) {
  .library-categories-container {
    padding: 10px;
  }
  
  .page-header {
    margin-bottom: 20px;
    
    h1 {
      font-size: 24px;
    }
  }
  
  .categories-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  
  .category-card {
    padding: 20px;
  }
}
</style>
